package com.atwisdom.star.core.bsl.planner.Impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.DateUtils;
import com.atwisdom.star.core.bsl.planner.WorkCalendarBslService;
import com.atwisdom.star.core.bsl.planner.entity.Holiday;
import com.atwisdom.star.core.dsl.modelClass.resource.Impl.ResourceInstanceDslServiceImpl;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Service
@NoArgsConstructor
public class WorkCalendarBslServiceImpl implements WorkCalendarBslService {

    private ResourceInstanceDslServiceImpl resourceInstanceDslService;

    @Autowired
    public void setResourceInstanceDslService(ResourceInstanceDslServiceImpl resourceInstanceDslService) {
        this.resourceInstanceDslService = resourceInstanceDslService;
    }

    //假期列表
    private List<String> holidayList = new ArrayList<>();
    //创建SimpleDateFormat对象，并指定日期格式
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //创建SimpleDateFormat对象，并指定日期格式
    private DateFormat dateFormatTwo = new SimpleDateFormat("yyyyMMdd");
    //全部假期
    private List<Holiday> holidays = new ArrayList<>();

    @Override
    public void initHoliday() {
        holidays = new ArrayList<>();
        String condition = "type=='tools' && className=='calendar_holiday'";
        List<JSONObject> calendarHolidayList = this.resourceInstanceDslService.query(condition, "calendar_holiday", null);
        for (JSONObject json : calendarHolidayList) {
            JSONObject values = json.getJSONObject("values");
            JSONArray dateRange = values.getJSONArray("tf_dateRange");
            String name = values.getString("sf_name");
            List<String> dateStrList = JSONUtil.parseArray(dateRange).toList(String.class);
            String startDateStr = dateStrList.get(0);
            String endDateStr = dateStrList.get(1);
            LocalDate startDate = DateUtils.ConvertDateStrToLocalDate(startDateStr);
            LocalDate endDate = DateUtils.ConvertDateStrToLocalDate(endDateStr);
            while (!startDate.isAfter(endDate)){
                Holiday holiday = new Holiday();
                holiday.setName(name);
                holiday.setDate(startDate);
                holidays.add(holiday);
                startDate = startDate.plusDays(1);
            }
        }
    }

    /**
     * 获取某年某月假日集合
     * @param year 年
     * @param month 月
     * @return 返回假日集合
     */
    @Override
    public List<Holiday> getHoliday(int year, int month) {
        LocalDate firstDate = LocalDate.of(year, month, 1);
        int daysInMonth = firstDate.lengthOfMonth();
        LocalDate lastDate = LocalDate.of(year, month, daysInMonth);
        return getHolidayList(firstDate, lastDate);
    }

    public List<Holiday> getHolidayList(LocalDate startDate, LocalDate endDate) {
        LocalDate startDateFinal = startDate.plusDays(-1);
        LocalDate endDateFinal = endDate.plusDays(1);
        List<Holiday> holidayList = this.holidays.stream().filter(h -> h.getDate().isAfter(startDateFinal) && h.getDate().isBefore(endDateFinal)).collect(Collectors.toList());
        return holidayList;
    }

    /**
     * 检查传入的日期是否是假日
     * @param localDate 检查日期
     * @return 判断结果，true：假日，false:非假日
     */
    @Override
    public Boolean checkHoliday(LocalDate localDate) {
        Holiday holiday = this.holidays.stream().filter(h -> h.getDate().isEqual(localDate)).findFirst().orElse(null);
        if(holiday!=null){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取工作日
     *
     * @param year  年
     * @param month 月
     * @return 获取工作日天数
     */
    @Override
    public int getWorkDay(int year, int month) {
        LocalDate firstDate = LocalDate.of(year, month, 1);
        int daysInMonth = firstDate.lengthOfMonth();
        List<Holiday> holidayList = this.getHoliday(year, month);
        int size = holidayList.size();
        return daysInMonth - size;
    }

    /**
     * 获取工作日天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 获取工作日天数
     */
    @Override
    public int getWorkDay(LocalDate startDate, LocalDate endDate) {
        int firstDays = startDate.getDayOfMonth();
        int lastDays = endDate.getDayOfMonth();
        List<Holiday> holidayList = this.getHolidayList(startDate, endDate);
        int size = holidayList.size();
        return lastDays - firstDays - size;
    }
}
